/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.comment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.OrderMapper;
import com.yami.shop.distribution.comment.dao.DistributionLevelMapper;
import com.yami.shop.distribution.comment.dao.DistributionUserMapper;
import com.yami.shop.distribution.comment.dto.DistributionUserAchievementDataDto;
import com.yami.shop.distribution.comment.model.DistributionLevel;
import com.yami.shop.distribution.comment.model.DistributionLevelSet;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.service.DistributionLevelService;
import com.yami.shop.distribution.comment.vo.DistributionAwardDataVO;
import com.yami.shop.distribution.comment.vo.DistributionLevelConditionDataVO;
import com.yami.shop.distribution.comment.vo.DistributionLevelConditionsSwitchVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @author lgh on 2019/04/04.
 */
@Service
@AllArgsConstructor
public class DistributionLevelServiceImpl extends ServiceImpl<DistributionLevelMapper, DistributionLevel> implements DistributionLevelService {

    private final DistributionUserMapper distributionUserMapper;

    private final OrderMapper orderMapper;

    private final DistributionLevelMapper distributionLevelMapper;


    @Override
    public void tryingToUpgrade(DistributionUser distributionUser, DistributionLevelSet distributionLevelSet) {
        DistributionLevel nextDistributionLevelInfo = getLevelInfo(distributionUser.getLevel() +1, distributionLevelSet.getDistributionLevels());
        if (nextDistributionLevelInfo == null) {
            // 您的等级已经达到上限了
            throw new YamiShopBindException("yami.distribution.level.limit");
        }

        //获取要升级等级的条件数据
        DistributionLevelConditionDataVO distributionLevelConditionData = nextDistributionLevelInfo.getDistributionLevelConditionDataVO();
        // 判断升级的开关
        DistributionLevelConditionsSwitchVO levelSetConditionsSwitch = distributionLevelSet.getLevelSetConditionsSwitch();

        //获取分销员与等级条件匹配的数据
        DistributionUserAchievementDataDto distributionUserAchievementDataDto=distributionUserMapper.getLevelDistributionUserAchievementDataByDistributionUserId(distributionUser.getDistributionUserId());

        if(!isConditions(distributionUserAchievementDataDto,distributionLevelConditionData,levelSetConditionsSwitch,distributionUser)){
            // 您不符合升级条件
            throw new YamiShopBindException("yami.distribution.level.condition");
        }
        // 升级
        distributionUserMapper.updateLevel(distributionUser.getDistributionUserId(),nextDistributionLevelInfo.getLevel());
    }



    @Override
    public DistributionLevel getLevelInfo(int level, List<DistributionLevel> distributionLevels) {
        if (level > 0 && level <= distributionLevels.size()) {
            return distributionLevels.get(level -1);
        }
        return null;
    }

    private boolean isConditions(DistributionUserAchievementDataDto distributionUserAchievementDataDto,
                                 DistributionLevelConditionDataVO distributionLevelConditionData,
                                 DistributionLevelConditionsSwitchVO levelSetConditionsSwitch, DistributionUser distributionUser) {
        //绑定客户数条件
        if(levelSetConditionsSwitch.getBoundCustomers()){
            if(distributionUserAchievementDataDto.getBoundCustomers() <  distributionLevelConditionData.getBoundCustomers()){
                return false;
            }
        }

        //邀请分销员数
        if(levelSetConditionsSwitch.getInvitedVeeker()){
            if(distributionUserAchievementDataDto.getInvitedVeeker() <  distributionLevelConditionData.getInvitedVeeker() ){
                return false;
            }
        }

        //支付单数
        if(levelSetConditionsSwitch.getPayNumber()){
            if(distributionUserAchievementDataDto.getPayNumber() <  distributionLevelConditionData.getPayNumber() ){
                return false;
            }
        }

        //积累收益
        if(levelSetConditionsSwitch.getAddupAmount()){
            if(distributionUserAchievementDataDto.getDistributionUserWallet().getAddupAmount() <  distributionLevelConditionData.getAddupAmount() ){
                return false;
            }
        }

        //成功成交单数
        if(levelSetConditionsSwitch.getSuccessOrderNumber()){
            if(distributionUserAchievementDataDto.getSuccessOrderNumber() <  distributionLevelConditionData.getSuccessOrderNumber() ){
                return false;
            }
        }

        //成功成交金额
        if(levelSetConditionsSwitch.getSuccessTradingVolume()){
            if(distributionUserAchievementDataDto.getSuccessTradingVolume() <  distributionLevelConditionData.getSuccessTradingVolume() ){
                return false;
            }
        }

        //成功成交金额
        if(levelSetConditionsSwitch.getSuccessTradingVolume()){
            if(distributionUserAchievementDataDto.getSuccessTradingVolume() <  distributionLevelConditionData.getSuccessTradingVolume() ){
                return false;
            }
        }

        //积累消费金额
        if(levelSetConditionsSwitch.getSumOfConsumption()){
            if(distributionUserAchievementDataDto.getSumOfConsumption() <  distributionLevelConditionData.getSumOfConsumption() ){
                return false;
            }
        }

        //积累充值金额
//        if(levelSetConditionsSwitch.getRechargeAmount()){
//            if(distributionUserAchievementDataDto.getRechargeAmount() <  distributionLevelConditionData.getRechargeAmount() ){
//                return false;
//            }
//        }

        //购买指定商品
        if(levelSetConditionsSwitch.getCommodity()){
            for(DistributionLevelConditionDataVO.CommodityItemData commodityItemData : distributionLevelConditionData.getCommodity()){
                // 判断有没有没有购买过指定商品
                if (orderMapper.hasBuySuccessProd(commodityItemData.getProdId(),distributionUser.getUserId()) == 0) {
                    return false;
                }
            }
        }
        return true;
    }
}
